﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Text;
using System.Data;
using ElingOA6_Admin_DSJCJ.Areas.Admin.Models;
using ElingOA6_Admin_DSJCJ.Common;

namespace ElingOA6_Admin_DSJCJ.Areas.Admin.Controllers
{
    /// <summary>
    /// 栏目考核分定义
    /// </summary>
    [OutputCache(Duration = 0, VaryByParam = "none")]
    public class DocClsScoreController : Common.AdminBaseController
    {
        //
        // GET: /Admin/Code/
        /// <summary>
        /// 栏目考核分定义
        /// </summary>
        /// <returns>视图</returns>
        [AuthorityActionFilter("m_DocClsScore", Order = 1)]
        [LogPageActionFilter("栏目考核分定义", Order = 2)]
        public ActionResult Index(string fYear)
        {
            if (fYear == null || fYear == "")
            {
                fYear = DateTime.Now.Year.ToString();
            }
            
            ViewData["dqyear"] = fYear;

            BLL.DocClsScore DocClsScoreBLL = new BLL.DocClsScore();

            DocClsScoreBLL.ExecPro(Common.GlobalConstant.ENTITYNO, int.Parse(fYear));

            DataSet ds = DocClsScoreBLL.getYears(Common.GlobalConstant.ENTITYNO);
            if (ds.Tables[0].Rows.Count == 0)
            {
                ViewData["minyear"] = fYear;
                ViewData["maxyear"] = fYear;
            }
            else
            {
                ViewData["maxyear"] = ds.Tables[0].AsEnumerable().Select(t => t.Field<decimal>("FYear")).Max();
                ViewData["minyear"] = ds.Tables[0].AsEnumerable().Select(t => t.Field<decimal>("FYear")).Min();
            }
            ViewData["ShowList"] = GetTable(fYear);
            ViewData["FYear"] = fYear;
            return View("Index");
        }

        public ActionResult Save()
        {
            string fYear = Fetch.Post("hdFYear");
            try
            {
                string[] fDocClsCds = Fetch.Post("FDocClsCd").Split(',');

                List<Model.DocClsScore> DocClsScoreList = new List<Model.DocClsScore>();
                for (int i = 0; i < fDocClsCds.Count(); i++)
                {
                    string fIsScore = "";
                    string fEnrollScore = Fetch.Post("FEnrollScore" + fDocClsCds[i]);
                    if (fEnrollScore == "")
                    {
                        fEnrollScore = "0";
                        fIsScore = "0";
                    }
                    else
                    {
                        fIsScore = "1";
                    }
                    Model.DocClsScore DocClsScoreModel = new Model.DocClsScore();
                    DocClsScoreModel.FIsScore = decimal.Parse(fIsScore);
                    DocClsScoreModel.FDocClsCd = fDocClsCds[i];
                    DocClsScoreModel.FEnrollScore = decimal.Parse(fEnrollScore);
                    DocClsScoreModel.FEntityNo = Common.GlobalConstant.ENTITYNO;
                    DocClsScoreModel.FYear = decimal.Parse(fYear);
                    DocClsScoreList.Add(DocClsScoreModel);
                }

                BLL.DocClsScore DocClsScoreBLL = new BLL.DocClsScore();
                DocClsScoreBLL.Update(DocClsScoreList);
                ViewData["jsAlert"] = "操作成功";
            }
            catch
            {
                ViewData["jsAlert"] = "操作异常";
            }
            
            if (fYear == null || fYear == "")
            {
                fYear = DateTime.Now.Year.ToString();
            }
            ViewData["FYear"] = fYear;
            ViewData["ShowList"] = GetTable(fYear);
            return View("Index");
        }

        public void Tabledg(string docClsCd, List<VDocClsScore> vDocClsScoreList, ref int i, ref StringBuilder str,int z)
        {
            string kgstr = "";
            for (int j = 0; j < z; j++)
            {
                kgstr += "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
            }
            List<VDocClsScore> select = (from nm in vDocClsScoreList where nm.FPreDocClsCd == docClsCd select nm).ToList();
            foreach (VDocClsScore model in select)
            {
                i++;
               
                str.Append("<tr id=''>");
                str.Append(" <td align='center' class='tdClass'>" + i + "</td>");
                str.Append(" <td align='center' class='tdClass'  style ='text-align:left'>" + model.FDocClsCd + "<input type='hidden' id='FDocClsCd' name='FDocClsCd' value='" + model.FDocClsCd + "'></td>");
                str.Append(" <td align='center' class='tdClass'  style ='text-align:left'>" + kgstr + model.FDocClsName + "</td>");
                if (model.FIsScore == 1)
                {
                    str.Append(" <td align='center' class='tdClass'><input type='checkbox' name='FIsScore" + model.FDocClsCd + "' id='FIsScore" + model.FDocClsCd + "' checked='checked' /></td>");
                    str.Append(" <td align='center' class='tdClass'><input type='text' id='FEnrollScore" + model.FDocClsCd + "' name='FEnrollScore" + model.FDocClsCd + "'  onkeyup=\"clearNoNum(this)\" onafterpaste=\"clearNoNum(this)\" value='" + model.FEnrollScore + "'></td>");
                }
                else
                {
                    str.Append(" <td align='center' class='tdClass'><input type='checkbox' name='FIsScore" + model.FDocClsCd + "' id='FIsScore" + model.FDocClsCd + "' /></td>");
                    str.Append(" <td align='center' class='tdClass'><input type='text' id='FEnrollScore" + model.FDocClsCd + "' name='FEnrollScore" + model.FDocClsCd + "'  onkeyup=\"clearNoNum(this)\" onafterpaste=\"clearNoNum(this)\" disabled='disabled' value='0'></td>");
                }

                str.Append("</tr>");

                Tabledg(model.FDocClsCd, vDocClsScoreList, ref i, ref str, z + 1);
            }
        }

        public string GetTable(string fYear)
        {
            StringBuilder str = new StringBuilder();
            BLL.DocClsScore docClsScoreBLL = new BLL.DocClsScore();
            DataSet ds =   docClsScoreBLL.GetDocClsScoreList(" FEntityNo='" + Common.GlobalConstant.ENTITYNO + "' and FYear='" + fYear + "'");
            List<VDocClsScore> vDocClsScoreList = DataTableToList(ds.Tables[0]);
            if (vDocClsScoreList.Count > 0)
            {
                int i = 0;
                List<VDocClsScore> select = (from nm in vDocClsScoreList where nm.FPreDocClsCd == "" || nm.FPreDocClsCd == null select nm).ToList();
                foreach (VDocClsScore model in select)
                {
                    i++;
                    str.Append("<tr id=''>");
                    str.Append(" <td align='center' class='tdClass'>" + i + "</td>");
                    str.Append(" <td align='center' class='tdClass' style ='text-align:left'>" + model.FDocClsCd + "<input type='hidden' id='FDocClsCd' name='FDocClsCd' value='" + model.FDocClsCd + "'></td>");
                    str.Append(" <td align='center' class='tdClass' style ='text-align:left'>" + model.FDocClsName + "</td>");
                    if (model.FIsScore == 1)
                    {
                        str.Append(" <td align='center' class='tdClass'><input type='checkbox' name='FIsScore" + model.FDocClsCd + "' id='FIsScore" + model.FDocClsCd + "' checked='checked' /></td>");
                        str.Append(" <td align='center' class='tdClass'><input type='text' id='FEnrollScore" + model.FDocClsCd + "' name='FEnrollScore" + model.FDocClsCd + "'  onkeyup=\"clearNoNum(this)\" onafterpaste=\"clearNoNum(this)\" value='" + model.FEnrollScore + "'></td>");
                    }
                    else
                    {
                        str.Append(" <td align='center' class='tdClass'><input type='checkbox' name='FIsScore" + model.FDocClsCd + "' id='FIsScore" + model.FDocClsCd + "' /></td>");
                        str.Append(" <td align='center' class='tdClass'><input type='text' id='FEnrollScore" + model.FDocClsCd + "' name='FEnrollScore" + model.FDocClsCd + "'  onkeyup=\"clearNoNum(this)\" onafterpaste=\"clearNoNum(this)\" disabled='disabled' value='0'></td>");
                    }
                    
                    str.Append("</tr>");
                    Tabledg(model.FDocClsCd, vDocClsScoreList,ref i,ref str,1);
                }
            }
            return str.ToString();
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<VDocClsScore> DataTableToList(DataTable dt)
        {
            List<VDocClsScore> modelList = new List<VDocClsScore>();
            int rowsCount = dt.Rows.Count;
            if (rowsCount > 0)
            {
                VDocClsScore model;
                for (int n = 0; n < rowsCount; n++)
                {
                    model = DataRowToModel(dt.Rows[n]);
                    if (model != null)
                    {
                        modelList.Add(model);
                    }
                }
            }
            return modelList;
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public VDocClsScore DataRowToModel(DataRow row)
        {
            VDocClsScore model = new VDocClsScore();
            if (row != null)
            {
                if (row["FEntityNo"] != null)
                {
                    model.FEntityNo = row["FEntityNo"].ToString();
                }
                if (row["FDocClsCd"] != null)
                {
                    model.FDocClsCd = row["FDocClsCd"].ToString();
                }
                if (row["FYear"] != null && row["FYear"].ToString() != "")
                {
                    model.FYear = decimal.Parse(row["FYear"].ToString());
                }
                if (row["FIsScore"] != null && row["FIsScore"].ToString() != "")
                {
                    model.FIsScore = decimal.Parse(row["FIsScore"].ToString());
                }
                if (row["FWriteScore"] != null && row["FWriteScore"].ToString() != "")
                {
                    model.FWriteScore = decimal.Parse(row["FWriteScore"].ToString());
                }
                if (row["FEnrollScore"] != null && row["FEnrollScore"].ToString() != "")
                {
                    model.FEnrollScore = decimal.Parse(row["FEnrollScore"].ToString());
                }
                if (row["FExamineScore"] != null && row["FExamineScore"].ToString() != "")
                {
                    model.FExamineScore = decimal.Parse(row["FExamineScore"].ToString());
                }
                if (row["FWriteNum"] != null && row["FWriteNum"].ToString() != "")
                {
                    model.FWriteNum = int.Parse(row["FWriteNum"].ToString());
                }
                if (row["FPreDocClsCd"] != null)
                {
                    model.FPreDocClsCd = row["FPreDocClsCd"].ToString();
                }
                if (row["FDocClsName"] != null)
                {
                    model.FDocClsName = row["FDocClsName"].ToString();
                }
                if (row["FSeq"] != null && row["FSeq"].ToString() != "")
                {
                    model.FSeq = int.Parse(row["FSeq"].ToString());
                }
            }
            return model;
        }

    }
}
